//
//  NYSubjectReDoViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/8.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYSubjectReDoViewController.h"
#import "NYSubjectTitleCell.h"
#import "NYSubjectAnswerCell.h"
#import "NYGoodSubjectActionCell.h"
#import "NYGoodAnalysisCell.h"
#import "NYGoodSubjectCorrectAnswerCell.h"
#import "NYReDoRequest.h"
#import "NYRedoSubmitAnswerRequest.h"
#import "NYSubjectAnswerSubmitCell.h"
#import "NYSubjectAnswerTextViewCell.h"
@interface NYSubjectReDoViewController ()
@property(nonatomic, strong) NYReDoModel *redoModel;
@property(nonatomic, strong) NICellObject *submitObject;
@end

@implementation NYSubjectReDoViewController

- (void)viewDidLoad
{
        [super viewDidLoad];
        [self loadData];
}
- (void)configTableSelected
{
        switch (self.redoModel.subject.question_type) {
                case NYQuestionTypeFillBlanks:
                {
                        self.tableView.allowsSelection = YES;
                }
                        break;
                case NYQuestionTypeSingleChoice:
                {
                        self.tableView.allowsSelection = YES;
                }
                        break;
                case NYQuestionTypeMultipleChoices:
                {
                        self.tableView.allowsSelection = YES;
                        self.tableView.allowsMultipleSelection = YES;
                }
                        break;
                        
                default:
                        break;
        }
}
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar
{
        [super configNavigationBar:navigationBar];
        self.title = @"继续答题";
}
- (void)loadData
{
        [self setLoading:YES animation:YES];
        [self configNimbus];
        
        
        NYReDoType type = NYReDoTypeGoodSubject;
        NSString *key = @"good_key";
        NSString *value = self.subjectModel.good_key;
        if (NULLString(self.subjectModel.good_key))
        {
                key = @"wrong_key";
                value = self.subjectModel.wrong_key;
                type = NYReDoTypeWrongSubject;
        }
        [[NYReDoRequest requestWithType:type param:@{
                                                     key : value
                                                     }] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYReDoModel *  _Nonnull data) {
                self.redoModel = data;
                [self configTableSelected];
                [self configNimbusData];
                [self setSuccess];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [self setError:error];
        }];
}
- (void)submitAnswer:(NYReDoModel *)redoModel
{
        NYReDoType type = NYReDoTypeGoodSubject;
        NSString *key = @"good_key";
        NSString *value = self.redoModel.good_key;
        if (NULLString(self.subjectModel.good_key))
        {
                key = @"wrong_key";
                value = self.redoModel.wrong_key;
                type = NYReDoTypeWrongSubject;
        }
        
        [[NYRedoSubmitAnswerRequest requestWithType:type param:@{
                                                                 key : value,
                                                                 @"answer" : @{@"answer1" : self.redoModel.subject.user_answer}.jsonStringEncoded
                                                                 }] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYReDoSubmitAnswerModel *  _Nonnull data) {
                [NYTipClass showText:data.info inView:self.view].willHideBlock = ^(UIView *hideInView, BOOL animated) {
                        [self backButtonDidClick:nil];
                };
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [NYTipClass showErrorWithText:error.localizedDescription inView:self.view].willHideBlock = ^(UIView *hideInView, BOOL animated) {
                        [self backButtonDidClick:nil];
                };
        }];
}

- (void)configNimbus
{
        [super configNimbus];
        [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:[UITableViewHeaderFooterView className]];
        
        /// 选择
        [self.tableViewActions attachToClass:[NICellObject class] tapBlock:^BOOL(NICellObject * object, NYSubjectReDoViewController * target, NSIndexPath *indexPath) {
                NYSubjectModel *subjectObj = object.userInfo;
                
                /// return YES 会调用 deselectRowAtIndexPath 方法， return NO 则不会调用
                /// 标题
                if (indexPath.row == 0)
                {
                        return YES;
                }
                /// 填空题
                if (subjectObj.question_type == NYQuestionTypeFillBlanks)
                {
                        return YES;
                }
                
                /// 最后一个 提交 Cell
                if (indexPath.row + 1 == [target.tableViewModel countInSection:indexPath.section]) {
                        return YES;
                }
                /// 单项选择题
                if (subjectObj.question_type == NYQuestionTypeSingleChoice)
                {
                        /// 判断当前是否有选项选择
                        [target.tableView.indexPathsForVisibleRows enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                if (obj.section == indexPath.section && obj.row != indexPath.row)
                                {
                                        [target.tableView deselectRowAtIndexPath:obj animated:NO];
                                }
                        }];
                        return NO;
                }
                /// 多选题
                return NO;
        }];
        
}
- (void)configNimbusData
{
        [self.tableViewModel addSectionWithTitle:nil];
        [self.tableViewModel addObject:[NICellObject objectWithCellClass:[NYSubjectTitleCell class] userInfo:self.redoModel.subject]];
        
        if (self.redoModel.subject.question_type != NYQuestionTypeFillBlanks)
        {
                [self.redoModel.subject.exercises_answer_arr enumerateObjectsUsingBlock:^(NSString * _Nonnull daanObj, NSUInteger daanIdx, BOOL * _Nonnull daanStop) {
                        [self.tableViewModel addObject:[NICellObject objectWithCellClass:[NYSubjectAnswerCell class] userInfo:self.redoModel.subject]];
                }];
        }
        else
        {
                [self.tableViewModel addObject:[NICellObject objectWithCellClass:[NYSubjectAnswerTextViewCell class] userInfo:self.redoModel.subject]];
        }
        
        self.submitObject =[NICellObject objectWithCellClass:[NYSubjectAnswerSubmitCell class] userInfo:self.redoModel];
        [self.tableViewModel addObject:self.submitObject];
        
        /// 提交
        [self.tableViewActions attachToObject:self.submitObject tapBlock:^BOOL(NICellObject * object, NYSubjectReDoViewController * target, NSIndexPath *indexPath) {
                [target submitAnswer:(NYReDoModel *)object.userInfo];
                return YES;
        }];
        
        [self.tableView reloadData];
        [self setSuccess];
}
- (void)configChoiceAnswer:( NYSubjectModel *)subjectObj atIndexPath:(NSIndexPath *)indexPath
{
        if (subjectObj.question_type != NYQuestionTypeFillBlanks)
        {
                subjectObj.user_answer = nil;
                NSMutableArray *resArr = @[].mutableCopy;
                [self.tableView.indexPathsForSelectedRows enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.section == indexPath.section)
                        {
                                [resArr addObject:@(obj.row).stringValue];
                        }
                }];
                subjectObj.user_answer = resArr;
        }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        /// 最后一个 提交 Cell 去掉
        if (indexPath.section == 0 && (indexPath.row + 1 < [self.tableViewModel countInSection:indexPath.section])) {
                NICellObject *obj = [self.tableViewModel objectAtIndexPath:indexPath];
                [self configChoiceAnswer:obj.userInfo atIndexPath:indexPath];
        }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
        /// 最后一个 提交 Cell 去掉
        if (indexPath.section == 0 && (indexPath.row + 1 < [self.tableViewModel countInSection:indexPath.section])) {
                NICellObject *obj = [self.tableViewModel objectAtIndexPath:indexPath];
                [self configChoiceAnswer:obj.userInfo atIndexPath:indexPath];
        }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
        UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[UITableViewHeaderFooterView className]];
        view.contentView.backgroundColor = UIColorForBackground;
        return view;
}
+ (NYViewControllerNavigationBarType)navigationBarType
{
        return NYViewControllerNavigationBarTypeBlue;
}

@end
